package org.thingsboard.server.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;

public class AdvancedEncryption {

    public static String encryptString(String input,String algorithm,String key) throws Exception {
        Key aesKey = new SecretKeySpec(key.getBytes(), algorithm);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);

        byte[] encryptedBytes = cipher.doFinal(input.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decryptString(String input, String algorithm, String key) throws Exception {
        Key aesKey = new SecretKeySpec(key.getBytes(), algorithm);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, aesKey);

        byte[] decryptedBytes = Base64.getDecoder().decode(input);
        byte[] originalBytes = cipher.doFinal(decryptedBytes);
        return new String(originalBytes);
    }
}
